/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.informet.ipd.modelo.dao;

import com.informet.ipd.modelo.vo.AvancesPorActividadVO;



import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author LAPTOP01
 */
public class CumplimientoTareaDao extends DBase{


  Connection conn = null;

    public CumplimientoTareaDao() {
        super();
    }

 
    public void  setTareasPorActividad(AvancesPorActividadVO bean, ResultSet rs) throws SQLException{
        //dtIniactc dtTeractc

           bean.setNUM_CODACTPER_PK(rs.getInt("NUM_CODACTPER_PK"));
           bean.setDT_INIACTPERC(rs.getString("dtIniTareac"));
           bean.setDT_TERACTPERC(rs.getString("dtTerTareac"));
           bean.setDT_TERACTPER(rs.getString("dtIniTarea"));
           bean.setDT_INIACTPER(rs.getString("dtTerTarea"));
          
         
           bean.setTX_DESCOBJ(rs.getString("TX_DESCOBJ"));
           bean.setTX_DESCOBJESP(rs.getString("TX_DESCOBJESP"));
           bean.setTX_DESCACTPER(rs.getString("TX_DESCACTPER"));
           bean.setTX_OBSERVACION(rs.getString("observacionTarea"));

          //Ini -->
           bean.setNUM_CODACTPER_PK(Integer.parseInt(rs.getString("NUM_CODACTPER_PK")));
           bean.setNumCodObj(Integer.parseInt(rs.getString("NUM_CODOBJ_PK")));
           bean.setNUM_CODOBJESP_PK(Integer.parseInt(rs.getString("NUM_CODOBJESP_PK")));
           //Fin -->

           bean.setNumCodTarea(Integer.parseInt(rs.getString("NUM_CODATAREA_PK")));
           bean.setDescTarea(rs.getString("tx_descatarea"));

           bean.setCodEtiqueta(Integer.parseInt(rs.getString("codEtiqueta")));

    }

      public void  setEtiquetas(AvancesPorActividadVO bean, ResultSet rs) throws SQLException{
           bean.setCodEtiqueta(Integer.parseInt(rs.getString("NUM_CODETIQUETATAREA_PK")));
           bean.setDescEtiqueta(rs.getString("tx_descetiquet"));
    }

     public List getListEtiquetas(String id,String trimestre)  throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<AvancesPorActividadVO> lista = new ArrayList<AvancesPorActividadVO>(0);

        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();

                sql = "  select * from ME_etiquetATAREA ";


            rs = stmt.executeQuery(sql);
            AvancesPorActividadVO bean = null;
            while (rs.next()) {
                bean = new AvancesPorActividadVO();
                setEtiquetas(bean,rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }


  public List getListTareasPorActividad(String idActividad,String idTarea)  throws SQLException {
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<AvancesPorActividadVO> lista = new ArrayList<AvancesPorActividadVO>(0);
        
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();          
//                 sql = " select * from me_actper "+
//                       " where 1=1 ";

                   sql = "  select "+
                         "           isnull(convert(varchar,at.dt_iniatarea,103),'') as dtIniTarea, "+
                         "           isnull(convert(varchar,at.dt_teratarea,103),'') as dtTerTarea, "+
                         "           isnull(convert(varchar,at.dt_iniatareac,103),'') as dtIniTareac, "+
                         "           isnull(convert(varchar,at.dt_teratareac,103),'') as dtTerTareac, "+
                         "           isnull(at.tx_observacion,'') as observacionTarea, "+
                         "           isnull(at.NUM_CODETIQUETATAREA_PK,'') as codEtiqueta, "+
                         "           * "+
                         "   from me_actper ac inner join me_atarea at on at.num_codactper_pk=ac.num_codactper_pk "+
                         "                     inner join me_objesp oe on oe.num_codobjesp_pk=ac.num_codobjesp_pk "+
                         "                     inner join me_obj o on o.num_codobj_pk=oe.num_codobj_pk "+

                         " where 1=1  "+
                         " and at.num_codactper_pk="+idActividad;
                 if(idTarea!=null && !idTarea.equals("-1"))
                  sql=sql+ " and at.num_codatarea_pk="+idTarea;

            rs = stmt.executeQuery(sql);
            AvancesPorActividadVO bean = null;
            while (rs.next()) {
                bean = new AvancesPorActividadVO();
                setTareasPorActividad(bean,rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }


      public String spMan(AvancesPorActividadVO bean) {
        String msj = "";
        
        try {


            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_updateMeAtarea(?,?,?,?,?,?)}");
            cs.setString(1, bean.getTipoOper());
            cs.setInt(2, bean.getNumCodTarea());
            cs.setString(3, bean.getDT_INIACTPERC());
            cs.setString(4, bean.getDT_TERACTPERC());
            cs.setString(5, bean.getTX_OBSERVACION());
            cs.setInt(6, bean.getCodEtiqueta());
            cs.execute();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
            msj = e.toString();
            return msj;
        }
        return msj;
    }
}